<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: HelpSystem.php 346 2010-01-31 10:22:06Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Common.Data.PortalHelpRecord');
Prado::using('Application.Common.Data.PortalLanguageRecord');

class HelpSystem extends PortalAdminPage
{
	private $_langid;
	private	$_langRecord;

	public function getDataItemCount()
	{
		$dr	= $this->DataAccess->createCommand("select * from tblhelp")->query();
		return $dr->RowCount;
	}

	public function dataBind()
	{
		$offset	= $this->HelpPageList->CurrentPageIndex * $this->HelpPageList->PageSize;
        $limit	= $this->HelpPageList->PageSize;

        if($offset + $limit > $this->HelpPageList->VirtualItemCount)
		{
            $limit = $this->HelpPageList->VirtualItemCount-$offset;
		}

		$sql = "SELECT tblhelp.*,tbllanguages.name as langname FROM tblhelp,tbllanguages WHERE tbllanguages.culture = tblhelp.culture order by tblhelp.pagename,tblhelp.culture limit $offset,$limit";
		try
		{
			$this->HelpPageList->DataSource	= $this->DataAccess->createCommand($sql)->query();
			$this->HelpPageList->dataBind();
		}
		catch(Exception $e)
		{
			$this->reportError(1,$e->getMessage());
		}
	}

	public function pageChanged($sender,$param)
    {
        $this->HelpPageList->CurrentPageIndex=$param->NewPageIndex;
        $this->dataBind();
    }

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle("Edit help pages");

		if(!$this->IsPostBack)
		{
			$this->HelpPageList->VirtualItemCount	= $this->DataItemCount;
			$this->dataBind();

			// Pages drop down
			$adminpages = PortalUtil::AvailablePagesInPath('Admin');
			foreach($adminpages as $adminpage)
			{
				$ap[] = array("id" => $adminpage['name'], "name" => $adminpage['name']);
			}

			$systempages = PortalUtil::AvailablePagesInPath('System');
			foreach($systempages as $systempage)
			{
				$ap[] = array("id" => $systempage['name'], "name" => $systempage['name']);
			}

			$this->PageName->Datasource	= $ap;
			$this->PageName->dataBind();

			//Culture drop down
			$cultures	= PortalLanguageRecord::finder()->findAll();
			foreach($cultures as $c)
			{
				if($c->active)
				{
					$culture[] = array("id" => $c->culture, "name" => $c->name);
				}
			}

			$this->PageCulture->DataSource	= $culture;
			$this->PageCulture->dataBind();
		}
	}

	public function AddHelpPage($sender, $param)
	{
		if($this->IsValid)
		{
			$lsRec				= new PortalHelpRecord();
			$lsRec->pagename	= $this->PageName->SelectedValue;
			$lsRec->culture		= $this->PageCulture->SelectedValue;
			$lsRec->title		= $this->strTitle->Text;
			$lsRec->content		= $this->HelpContent->Text;
			$lsRec->save();
			$this->gotoPage('Admin.HelpSystem');
		}
	}

	public function deleteItem($sender, $param)
	{
		$id = $this->HelpPageList->DataKeys[$param->Item->ItemIndex];
		$lsRec	= PortalHelpRecord::finder()->findByid($id);

		if($lsRec)
		{
			$lsRec->delete();
			$this->gotoPage('Admin.HelpSystem');
		}

		$this->dataBind();
	}

	public function itemCommand($s,$p)
	{
	}
}
?>